package com.woniu51.search.service;

import com.netflix.discovery.converters.Auto;
import com.woniu.cinema.api.CinemaFeignClient;
import com.woniu.cinema.domain.Actor;
import com.woniu.cinema.domain.Cinema;
import com.woniu.cinema.vo.MovieVo;
import com.woniu51.search.domain.ActorSearch;
import com.woniu51.search.domain.CinemaSearch;
import com.woniu51.search.domain.MovieSearch;
import com.woniu51.search.repository.ActorSearchRepository;
import com.woniu51.search.repository.CinemaSearchRepository;
import com.woniu51.search.repository.MovieSearchRepository;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 杨光
 * @date 2022年04月12日 10:57
 */
@Service
public class SearchService {

    @Autowired
    CinemaFeignClient cinemaFeignClient;

    @Autowired
    ActorSearchRepository actorSearchRepository;

    @Autowired
    MovieSearchRepository movieSearchRepository;

    @Autowired
    CinemaSearchRepository cinemaSearchRepository;


    public String initEs(){
        refreshActors(-1);
        refreshCinemas(-1);
        refreshMovie(-1);
        return "ok";
    }

    public void refreshActors(Integer actorId){
        List<ActorSearch> actorSearchList = cinemaFeignClient.queryById(actorId)
                .stream().map(a -> {
                    ActorSearch actorSearch = new ActorSearch();
                    BeanUtils.copyProperties(a, actorSearch);

                    actorSearch.setSearchText(a.getActorName() + " " + a.getActorDesc());
                    return actorSearch;
        }).collect(Collectors.toList());

        actorSearchRepository.saveAll(actorSearchList);


    }
    public void refreshCinemas(Integer cinemaId){
        List<CinemaSearch> cinemaSearchList = cinemaFeignClient.queryCinemaById(cinemaId).stream().map(c -> {
            CinemaSearch cinemaSearch = new CinemaSearch();
            //把cinema里面的同名属性复制到cinemaSearch对象
            BeanUtils.copyProperties(c, cinemaSearch);
            cinemaSearch.setSearchText(c.getCinemaName() + " " + c.getCinemaLocation());
            return cinemaSearch;
        }).collect(Collectors.toList());

        cinemaSearchRepository.saveAll(cinemaSearchList);

    }
    public void refreshMovie(Integer movieId){
        List<MovieVo> movieVoList = cinemaFeignClient.queryMovieById(movieId);
        List<MovieSearch> movieSearchList = movieVoList.stream().map(m -> {
            MovieSearch movieSearch = new MovieSearch();
            BeanUtils.copyProperties(m, movieSearch);
            movieSearch.setMovieTypeStr(m.getMovieTypeStr());
            movieSearch.setSearchText(m.getMovieInfo() + " " + m.getMovieName() + " " + m.getMovieEname() + " " + m.getMovieTypeStr());

            return movieSearch;

        }).collect(Collectors.toList());

        movieSearchRepository.saveAll(movieSearchList);
    }

    public List<ActorSearch> searchActorByKey(String searchKey) {
        QueryBuilder builder = QueryBuilders.matchQuery("searchText", searchKey);
        Iterable<ActorSearch> actorSearches = actorSearchRepository.search(builder);
        List<ActorSearch> list = new LinkedList();

        for (ActorSearch actorSearch : actorSearches) {
            list.add(actorSearch);
        }
        return list;
    }

    public List<MovieSearch> searchMovieByKey(String searchKey) {
        QueryBuilder builder = QueryBuilders.matchQuery("searchText", searchKey);
        Iterable<MovieSearch> movieSearches = movieSearchRepository.search(builder);
        List<MovieSearch> list = new LinkedList<>();
        for (MovieSearch movieSearch : movieSearches) {
            list.add(movieSearch);
        }
        return list;
    }

    public List<CinemaSearch> searchCinameByKey(String searchKey) {
        QueryBuilder builder = QueryBuilders.matchQuery("searchText", searchKey);
        Iterable<CinemaSearch> cinemaSearches = cinemaSearchRepository.search(builder);
        List<CinemaSearch> list = new LinkedList<>();
        for (CinemaSearch cinemaSearch : cinemaSearches) {
            list.add(cinemaSearch);
        }
        return list;
    }
}
